package com.zcc.makingfriend.service.impl;

import com.zcc.makingfriend.dao.Liked;
import com.zcc.makingfriend.dto.ArticleLikedDto;
import com.zcc.makingfriend.mapper.ArticleMapper;
import com.zcc.makingfriend.mapper.LikedMapper;
import com.zcc.makingfriend.service.LikedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zcc
 */
@Service
public class LikedServiceImpl implements LikedService {

    @Autowired
    LikedMapper likedMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public int addLiked(ArticleLikedDto articleLikedDto) {
        Liked l = likedMapper.getLiked(articleLikedDto.getArticleId(), articleLikedDto.getUserId());
        if (l == null) {
            Liked liked = new Liked(articleLikedDto.getArticleId(), articleLikedDto.getUserId());
            int i = likedMapper.addLiked(liked);
            int likes = articleLikedDto.getLikes() + 1;
            int likeArticle = articleMapper.isLikeArticle(likes, articleLikedDto.getArticleId());
            if (likeArticle == 1 && i == 1) {
                return likes;
            }
            return articleLikedDto.getLikes();
        }
        return -1;
    }

    @Override
    public int cancelLiked(ArticleLikedDto articleLikedDto) {
        Liked liked = new Liked(articleLikedDto.getArticleId(), articleLikedDto.getUserId());
        int i = likedMapper.cancelLiked(liked);
        int likes = articleLikedDto.getLikes() - 1;
        int likeArticle = articleMapper.isLikeArticle(likes, articleLikedDto.getArticleId());
        if (likeArticle == 1 && i == 1) {
            return likes;
        }
        return articleLikedDto.getLikes();
    }
}
